import { cloud } from '@hw-agconnect/cloud-server';
import { location } from './location'; // location是Server Model
// default是location对象类型使用的存储区名
// 创建链接数据库的句柄
const collection = cloud.database({ zoneName: 'default' }).collection(location);

interface IUpsertRes {
  result: number
}

export class LocationObj {
  query() {
    return collection.query().get();
  }


  upset(locations: location[], isInsert: boolean) {

    console.log('JSON:' + JSON.stringify(locations))
    const mapsArr = locations.map(lt => location.parseFrom(lt))
    console.log('JSON2:' + JSON.stringify(mapsArr))

    return new Promise<IUpsertRes>((resolve, reject) => {
      collection.upsert(locations.map(lt => location.parseFrom(lt)))
        .then(result => resolve({ result }))
        .catch(err => reject(err))
    });
  }
}
